﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Collections.Generic;

namespace hzWeb.MeetingRecordsEventReceiver
{
    /// <summary>
    /// 列表项事件
    /// </summary>
    public class MeetingRecordsEventReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// 正在添加项.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           if (properties.List == null || properties.List.BaseTemplate.ToString() != "10000") return;
        
           // base.ItemAdding(properties);
           // string fileExtention = "wmv;wma;avi;mpg;mp3;";
           // properties.ErrorMessage = "wmv;wma;avi;mpg;mp3;";
           // properties.Cancel = true;
           string[] extenions = {"wmv", "wma", "avi","mpg", "mp3", "mp4"};
           string beforeUrl = properties.BeforeUrl;
           string extension = beforeUrl.Substring(beforeUrl.LastIndexOf('.') + 1);
           List<string> extenionsList = new List<string>(extenions);
           if (!extenionsList.Contains(extension))
           {
               properties.ErrorMessage = "对不起，只允许上传 wmv;wma;avi;mpg;mp3;mp4";
               properties.Cancel = true;
               return;
           }

           var meetingHoster = properties.AfterProperties["MeetingHoster"];
           if (meetingHoster == null)
           {
               SPUser user = properties.Web.CurrentUser;
               // SPFieldLookupValue lookUpValue = new SPFieldLookupValue(user.ID, user.Name);
               //SPFieldUserValue userFiled = new SPFieldUserValue(properties.Web);
               //userFiled.LookupId = user.ID;
               // userFiled.LookupValue = user.Name;

               // string defaultValue = string.Format("{0};#{1}", user.ID.ToString(), user.Name);
               properties.AfterProperties.ChangedProperties.Add("MeetingHoster", user.ID);
               // MeetingSubject
               // properties.AfterProperties.ChangedProperties.Add("MeetingSubject", user.LoginName);
           }
           
       }

       /// <summary>
       /// 正在更新项.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           if (properties.List == null || properties.List.BaseTemplate.ToString() != "10000") return;
           // base.ItemUpdating(properties);
       }

       /// <summary>
       /// 正在删除项.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           if (properties.List == null || properties.List.BaseTemplate.ToString() != "10000") return;
           // base.ItemDeleting(properties);
       }

       /// <summary>
       /// 已添加项.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           if (properties.List == null || properties.List.BaseTemplate.ToString() != "10000") return;
           // base.ItemAdded(properties);
       }


    }
}
